package com.jse.util;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.jse.lang.util.NutMap;

public class JseMap extends NutMap {
	
	private static final long serialVersionUID = 1L;
	
	public JseMap() {
	super();
	}
	public static JseMap WRAP(Map<String, Object> map) {
		if (null == map)
            return null;
        if (map instanceof JseMap)
            return (JseMap) map;
        return new JseMap(map);
	}
	
	public JseMap(String key, Object value) {
		super(key,value);
	}

	public JseMap(Map<String, Object> map) {
		super();
        this.putAll(map);
	}

	public JseMap getMap(String key) {
		return getAs(key, this.getClass());
	}
	
	public static JseMap make(String key, Object value) {
   	 return new JseMap(key,value);
	}
   
   public static JseMap from(Map<String, Object> map) {
       return WRAP(map);
   }
   public JseMap set(String key, Object value) {
       this.put(key, value);
       return this;
   }
   public JseMap add(String key, Object value) {
       this.put(key, value);
       return this;
   }
   public JseMap put(String key, Object value) {
       super.put(key, value);
       return this;
   }
   
   /**
    * 为 Map 增加一个名值对。如果同名已经有值了，那么会将两个值合并成一个列表
    * 
    * @param key
    * @param value
    */
   @Override
   @SuppressWarnings("unchecked")
   public JseMap addv(String key, Object value) {
       Object obj = get(key);
       if (null == obj) {
           put(key, value);
       } else if (obj instanceof List<?>)
           ((List<Object>) obj).add(value);
       else {
           List<Object> list = new LinkedList<Object>();
           list.add(obj);
           list.add(value);
           put(key, list);
       }
       return this;
   }
}
